﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Diagnostics;

namespace CSC440TeamOne.MyHealth.Views
{
    public partial class DailyEntry : Microsoft.Practices.CompositeWeb.Web.UI.Page, IDailyEntryView
    {
        private DailyEntryPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //if (Page.User.Identity.IsAuthenticated)
                //{
                mvState.SetActiveView(vwEntry);
                //}

                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            repHealParameters.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(repHealParameters_ItemDataBound);

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            _presenter.OnSave();
        }

        void repHealParameters_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, TrackableHealthParameter>.Bind(e.Item, a =>
            {
                a.Find<Label>("lblParameterName").Text = a.BusinessItem.Name;

                a.Find<TextBox>("txtParameterValue").Attributes.Add("HealthParameterID", a.BusinessItem.ID.ToString());
            });
        }

        [CreateNew]
        public DailyEntryPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public void Show(Common.DailyEntryViewModel model)
        {
            IEnumerable<TrackableHealthParameter> healthParameters = model.HealthParameters;

            if (healthParameters.Any())
            {
                repHealParameters.DataSource = healthParameters;

                repHealParameters.DataBind();
            }
        }


        public void Update(Common.DailyEntryViewModel model)
        {
            foreach (RepeaterItem repeaterItem in repHealParameters.Items)
            {
                if (repeaterItem.ItemType == ListItemType.AlternatingItem || repeaterItem.ItemType == ListItemType.Item)
                {
                    var txtParameterValue = repeaterItem.FindControl("txtParameterValue") as TextBox;

                    if (txtParameterValue != null)
                    {
                        var HealthParameterID = txtParameterValue.Attributes["HealthParameterID"];

                        if (!string.IsNullOrEmpty(HealthParameterID))
                        {
                            var id = int.MinValue;

                            if (int.TryParse(HealthParameterID, out id))
                            {
                                double tempDouble = double.MinValue;

                                if (double.TryParse(txtParameterValue.Text, out tempDouble))
                                {
                                    model.UpdateHealthParameter(id, tempDouble);
                                }
                            }
                        }
                    }
                }
            }
        }


        public void OnSaveCompleted(Common.DailyEntryViewModel model)
        {
            mvState.SetActiveView(vwConfirm);
        }
    }
}

